//
//  TPArcView.m
//  CoreAnimationDemo
//
//  Created by 谭鹏 on 2017/6/8.
//  Copyright © 2017年 谭鹏. All rights reserved.
//

#import "TPArcView.h"
@interface TPArcView()
//@property CGFloat lineWidth;
////@property CGPoint arcCenter;
//@property CGFloat radius;
//
//@property NSArray<NSString *> * numberTexts;
//@property NSArray<UIColor *> * colors;
//@property NSMutableArray<UIBezierPath *> * paths;
@property CGFloat arcStartAngle;
@property CGFloat sumOfNumber;
@property NSUInteger selectedArcIndex;
@end
@implementation TPArcView
- (instancetype)init
{
    self = [super init];
    if (self) {
        _sumOfNumber = 0;
        _arcStartAngle = -M_PI;
        _selectedArcIndex = 0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapArc:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    self.backgroundColor = [UIColor clearColor];
    _paths = [NSMutableArray new];
    
    for (NSString * number in _numberTexts) {
        _sumOfNumber += number.floatValue;
        
    }
    CGFloat lastlayerEndAngle = _arcStartAngle ;
    for (int i = 0; i < _numberTexts.count; i ++) {
        NSString * numberText = _numberTexts[i];
        CGFloat thisLayerEndAngle = lastlayerEndAngle + 2*M_PI*(numberText.floatValue/_sumOfNumber);//这个弧线
        UIBezierPath * path = [UIBezierPath new];
        [path addArcWithCenter:CGPointMake(rect.size.width/2.0, rect.size.height/2.0) radius:_radius startAngle:lastlayerEndAngle endAngle:thisLayerEndAngle clockwise:YES];
        path.lineWidth = _lineWidth;
        [_colors[i] set];
        [path stroke];
//        [path fill];
        [_paths addObject:path];
        lastlayerEndAngle = thisLayerEndAngle;
    }
    
}
-(void)tapArc:(UITapGestureRecognizer *)tapGR{
    
    CGPoint touchPoint = [tapGR locationInView:self];
    CGFloat deltaY = touchPoint.y - self.center.y;
    CGFloat deltaX = touchPoint.x - self.center.x;
    CGFloat deltaToCenter = hypot(deltaX, deltaY);//直角三角形的斜边
    if (deltaToCenter < _radius - _lineWidth/2.0) {
        NSLog(@"点击了中心");
        [self tapArcWithIndex:_selectedArcIndex++ %_numberTexts.count ];
        return;
    }
    CGFloat atan = atan2f(deltaY, deltaX);//算出角度 -PI ~ PI

    
    
    CGFloat lastlayerEndAngle = _arcStartAngle ;
    if (atan < _arcStartAngle) {
        atan += 2* M_PI;
    }
    for (NSString * numberText in _numberTexts) {
         CGFloat thisLayerEndAngle = lastlayerEndAngle + 2*M_PI*(numberText.floatValue/_sumOfNumber);//这个弧线
        
//        thisLayerEndAngle = thisLayerEndAngle >  M_PI ? thisLayerEndAngle - 2* M_PI : thisLayerEndAngle;
        NSLog(@"angle : %f start:%f end:%f",atan,lastlayerEndAngle,thisLayerEndAngle);
        
        if (atan>lastlayerEndAngle && atan < thisLayerEndAngle) {
            
            CGFloat angle = M_PI/2 - (lastlayerEndAngle + thisLayerEndAngle)/2;
            [self animatedWithAngle: angle];
            NSLog(@"点击了 ：%@",numberText);
            return;
        }
        lastlayerEndAngle = thisLayerEndAngle;
    }
}
-(void)tapArcWithIndex:(NSUInteger)index{
    if (index >= _numberTexts.count) {
        NSLog(@"超界");
    }else{
        CGFloat startSum = 0;
        for (int i = 0; i < index; i ++) {
            startSum += _numberTexts[i].floatValue;
        }
        CGFloat angle =  M_PI/2 - (  _arcStartAngle +  2*M_PI*(startSum + _numberTexts[index].floatValue/2)/_sumOfNumber ) ;
        [self animatedWithAngle: angle];
    }
    
}
-(void)animatedWithAngle:(CGFloat)angle{
    CGFloat fromPointAngle = _arcStartAngle + M_PI ;
    CGFloat toPointAngle = fromPointAngle  + angle;
    
    CABasicAnimation *circleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    circleAnimation.duration = 1.f;
    circleAnimation.repeatCount = 1;
    circleAnimation.fromValue = @(fromPointAngle);
    circleAnimation.toValue = @(toPointAngle);
    circleAnimation.fillMode = kCAFillModeForwards;
    circleAnimation.removedOnCompletion = NO;
    circleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut ];
    [self.layer addAnimation:circleAnimation forKey:@"rotation"];
    
    
    _arcStartAngle = _arcStartAngle + angle  ;
    _arcStartAngle = _arcStartAngle >  M_PI ? _arcStartAngle - 2* M_PI : _arcStartAngle;
}
@end
